package com.woniu.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniu.entity.Auctiongoods;
import com.woniu.entity.Goods;
import com.woniu.entity.Order;
import com.woniu.entity.Record;
import com.woniu.utils.MyPage;
import com.woniu.vo.OrderDataVo;
import com.github.pagehelper.PageHelper;
import com.woniu.dao.OrderMapper;

@Transactional
@Service
public class OrderService {
	@Autowired
	private OrderMapper orderMapper;

	public OrderMapper georderMapper() {
		return orderMapper;
	}
	
	/**
	 * @return 累计成交金额
	 */
	public Double getTotalDealAmount() {
		// TODO Auto-generated method stub
		if (orderMapper.getTotalDealAmount() == null) {
			return 0.0;
		} else {
			return orderMapper.getTotalDealAmount();
		}
	}

	/**
	 * @return 今日成交金额
	 */
	public Double getDealAmountToday() {
		// TODO Auto-generated method stub
		if (orderMapper.getDealAmountToday() == null) {
			return 0.0;
		} else {
			return orderMapper.getDealAmountToday();
		}
	}

	/**
	 * @return 累计成交商品数
	 */
	public Integer getTotalDealProductCount() {
		// TODO Auto-generated method stub
		if (orderMapper.getTotalDealProductCount() == null) {
			return 0;
		}
		return orderMapper.getTotalDealProductCount();
	}

	/**
	 * @return 今日成交商品数
	 */
	public Integer getTodayDealProductCount() {
		// TODO Auto-generated method stub
		if (orderMapper.getTodayDealProductCount() == null) {
			return 0;
		}
		return orderMapper.getTodayDealProductCount();
	}

	/**
	 * @return 累计成交用户数
	 */
	public Integer getDealUserCount() {
		// TODO Auto-generated method stub
		if (orderMapper.getDealUserCount() == null) {
			return 0;
		}
		return orderMapper.getDealUserCount();
	}

	/**
	 * @return 当日流水
	 */
	public Double getTradingFlowToday() {
		// TODO Auto-generated method stub
		if (orderMapper.getTradingFlowToday() == null) {
			return 0.0;
		}
		return orderMapper.getTradingFlowToday();
	}

	/**
	 * @return
	 */
	public Double getTradingFlowYesterday() {
		// TODO Auto-generated method stub
		if (orderMapper.getTradingFlowYesterday() == null) {
			return 0.0;
		}
		return orderMapper.getTradingFlowYesterday();
	}

	/**
	 * @return
	 */
	public Double getTradingFlowThisWeek() {
		// TODO Auto-generated method stub
		if (orderMapper.getTradingFlowThisWeek() == null) {
			return 0.0;
		}
		return orderMapper.getTradingFlowThisWeek();
	}

	/**
	 * @return
	 */
	public Double getTradingFlowThisMonth() {
		// TODO Auto-generated method stub
		if (orderMapper.getTradingFlowThisMonth() == null) {
			return 0.0;
		}
		return orderMapper.getTradingFlowThisMonth();
	}

	/**
	 * @return
	 */
	public Double commissionRevenueToday() {
		// TODO Auto-generated method stub
		if (orderMapper.commissionRevenueToday() == null) {
			return 0.0;
		}
		return orderMapper.commissionRevenueToday();
	}

	/**
	 * @return
	 */
	public Double commissionRevenueYesterday() {
		// TODO Auto-generated method stub
		if (orderMapper.commissionRevenueYesterday() == null) {
			return 0.0;
		}
		return orderMapper.commissionRevenueYesterday();
	}

	/**
	 * @return
	 */
	public Double commissionRevenueThisWeek() {
		// TODO Auto-generated method stub
		if (orderMapper.commissionRevenueThisWeek() == null) {
			return 0.0;
		}
		return orderMapper.commissionRevenueThisWeek();
	}

	/**
	 * @return
	 */
	public Double commissionRevenueThisMonth() {
		// TODO Auto-generated method stub
		if (orderMapper.commissionRevenueThisMonth() == null) {
			return 0.0;
		}
		return orderMapper.commissionRevenueThisMonth();
	}

	/**
	 * @return 今日成交金额
	 */
	public Double dealAmountToday() {
		// TODO Auto-generated method stub
		if (orderMapper.dealAmountToday() == null) {
			return 0.0;
		}
		return orderMapper.dealAmountToday();
	}

	/**
	 * @return 昨日成交金额
	 */
	public Double dealAmountYesterday() {
		// TODO Auto-generated method stub
		if (orderMapper.dealAmountYesterday() == null) {
			return 0.0;
		}
		return orderMapper.dealAmountYesterday();
	}

	/**
	 * @return 本周成交金额
	 */
	public Double dealAmountThisWeek() {
		// TODO Auto-generated method stub
		if (orderMapper.dealAmountThisWeek() == null) {
			return 0.0;
		}
		return orderMapper.dealAmountThisWeek();
	}

	/**
	 * @return 本月成交金额
	 */
	public Double dealAmountThisMonth() {
		// TODO Auto-generated method stub
		if (orderMapper.dealAmountThisMonth() == null) {
			return 0.0;
		}
		return orderMapper.dealAmountThisMonth();
	}

	/**
	 * @return 今日成交订单数
	 */
	public Integer dealOrderCountToday() {
		// TODO Auto-generated method stub
		if (orderMapper.dealOrderCountToday() == null) {
			return 0;
		}
		return orderMapper.dealOrderCountToday();
	}

	/**
	 * @return 昨日成交订单数
	 */
	public Integer dealOrderCountYesterday() {
		// TODO Auto-generated method stub
		if (orderMapper.dealOrderCountYesterday() == null) {
			return 0;
		}
		return orderMapper.dealOrderCountYesterday();
	}

	/**
	 * @return 本周成交订单数
	 */
	public Integer dealOrderCountThisWeek() {
		// TODO Auto-generated method stub
		if (orderMapper.dealOrderCountThisWeek() == null) {
			return 0;
		}
		return orderMapper.dealOrderCountThisWeek();
	}

	/**
	 * @return 本月成交订单数
	 */
	public Integer dealOrderCountThisMonth() {
		// TODO Auto-generated method stub
		if (orderMapper.dealOrderCountThisMonth() == null) {
			return 0;
		}
		return orderMapper.dealOrderCountThisMonth();
	}

	/**
	 * @return
	 */
	public List<Map> getDealAmountByDate() {
		// TODO Auto-generated method stub
		return orderMapper.getDealAmountByDate();
	}

	/**
	 * @return
	 */
	public List<Map> getOrderCountByDate() {
		// TODO Auto-generated method stub
		return orderMapper.getOrderCountByDate();
	}

	/**
	 * 
	 * @param bname       买家姓名
	 * @param sname       卖家姓名
	 * @param firstTime   起始时间
	 * @param lastTime    结束时间
	 * @param pageNum     当前为第几页
	 * @param pageSize    每页显示几条
	 * @param timeChoose  按时间排序的正倒序选择
	 * @param moneyChoose 按金额排序的正倒序选择
	 * @return 返回每页显示的数据集合
	 * @throws ParseException
	 */
	public MyPage<OrderDataVo> selectByCond(String bname, String sname, String date1, int pageNum, int pageSize,
			int timeChoose, int moneyChoose) throws Exception {
		// 判断前端传来的日期是否为空
		if (!"".equals(date1)) {
			// 不为空就将时间拆分成起始时间和结束时间，查询区间之间的数据
			Date firstTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.parse(date1.substring(0, date1.indexOf("至")).trim());
			Date lastTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.parse(date1.substring(date1.indexOf("至") + 1).trim());
			return this.getPage(bname, sname, firstTime, lastTime, pageNum, pageSize, timeChoose, moneyChoose);
		} else {
			// 为空就将首尾时间赋值为null
			Date firstTime = null;
			Date lastTime = null;
			return this.getPage(bname, sname, firstTime, lastTime, pageNum, pageSize, timeChoose, moneyChoose);
		}
	}

	/**
	 * 根据前端接收到的值到数据库查询数据
	 */
	private MyPage<OrderDataVo> getPage(String bname, String sname, Date firstTime, Date lastTime, int pageNum,
			int pageSize, int timeChoose, int moneyChoose) {
		// 根据买卖家姓名查到order表中对应的买卖家id
		Integer bid = orderMapper.selectByUname(bname);
		Integer sid = orderMapper.selectByUname(sname);
		// 得到根据条件查询到的数据数量
		int count = orderMapper.selectOrderByBidAndSid(bid, sid, firstTime, lastTime, timeChoose, moneyChoose).size();
		// 创建一个vo类的集合，用于存放包括order类和一些其他字段
		List<OrderDataVo> orderDataVoList = new ArrayList<OrderDataVo>();
		// 框架自带的分页工具，根据当前页和每页显示的数量将紧随其后查询到的集合分成所需要的每页数据
		PageHelper.startPage(pageNum, pageSize);
		List<Order> list = orderMapper.selectOrderByBidAndSid(bid, sid, firstTime, lastTime, timeChoose, moneyChoose);
		// 将查询出的order数据存放到vo类集合中
		for (Order o : list) {
			String buyName = orderMapper.selectNameById(o.getuBid());
			String sellName = orderMapper.selectNameById(o.getuSid());
			String gName = orderMapper.selectGnameByGid(o.getGid());
			double deposit = orderMapper.selectDepositByGid(o.getGid());
			OrderDataVo odv = new OrderDataVo();
			odv.setOrder(o);
			odv.setBname(buyName);
			odv.setSname(sellName);
			odv.setGname(gName);
			odv.setDeposit(deposit);
			orderDataVoList.add(odv);
		}
		// 框架自带的工具类:将返回结果包装成分页所需要的数据
		MyPage<OrderDataVo> page = new MyPage<OrderDataVo>(count, pageSize, orderDataVoList);
		return page;
	}

	public int deleteOrderByOid(Integer oid) {
		// TODO Auto-generated method stub
		return orderMapper.deleteOrderByOid(oid);
	}
	
	public void insertOrder(Integer uBid, Integer uSid, Integer getid, double dealPrice,
			double commision, double orderTotalPrice, Date dealTime, Integer gid, int buyCount, String orderStatus,
			String paymentChannel) {
		String temp = Integer.toUnsignedString(UUID.randomUUID().hashCode());
		long temp1 = Long.parseLong(temp);
		long temp2 = 17*temp1;
		String orderGenerateld = temp1+""+temp2;
		Order order = new Order();
		order.setOrdergenerateid(orderGenerateld);
		order.setuBid(uBid);
		order.setuSid(uSid);
		order.setGetid(getid);
		order.setDealprice(dealPrice);
		order.setCommision(commision);
		order.setOrdertotalprice(orderTotalPrice);
		order.setDealtime(dealTime);
		order.setGid(gid);
		order.setBuycount(buyCount);
		order.setOrderstatus(orderStatus);
		order.setPaymentchannel(paymentChannel);
		order.setOrdertrade("");
		orderMapper.insert(order);
	}


	public List<Auctiongoods> selectAuctiongoods() {
		// TODO Auto-generated method stub
		return orderMapper.selectAuctiongoods();
	}

	public List<Goods> selectGoods(Integer gid) {
		// TODO Auto-generated method stubs
		return orderMapper.selectGoods(gid);
	}

	public Record selectRecord(Integer gid) {
		// TODO Auto-generated method stub
		return orderMapper.selectRecord(gid);
	}

	public Integer selectGetidByUsid(Integer uSid) {
		// TODO Auto-generated method stub
		return orderMapper.selectGetidByUsid(uSid);
	}

	public double selectDepositByAgid(Integer agid) {
		// TODO Auto-generated method stub
		return orderMapper.selectDepositByAgid(agid);
	}

//	public void updateGoodstatus(Integer gid) {
//		// TODO Auto-generated method stub
//		orderMapper.updateGoodstatus(gid);
//	}


}